home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
prolog
/
modprolg
/
mod-prol.lha
/
Prolog
/
Examples
/
database.mod
< prev
next >
Wrap
Text File
|
1992-06-15
|
5KB
|
148 lines
% This is an example of a database program. Although it is a rather
% small and very artificial example, it shows of many of the facilities
% available under the modules system. The program itself is very dirty
% however, and is an example of good modular programming.
% For example :
% Asserting into remote structures.
% Calling predicates in remote structures.
% The use of term manipulation predicates.
% I/O Predicates.
% Moving atoms to remote structures (using dismantle_name/3).
signature dataopsig =
sig
fun record/2.
pred add_record/0 and get_record/1.
end.
signature searchsig =
sig
pred ismatch/3.
end.
structure database =
struct
pred record/2.
end.
structure dataoperations/dataopsig =
struct
fun record/2.
add_record :-
writename('Type name'),nl,
get_atom(A),
writename('Type search keys (terminate with end).'), nl,
get_keys(List),
B =.. [data|List],
structure(Tag,database),
assert(record(A,B),Tag).
get_record(record(X,Y)) :-
var(X), var(Y),
structure(Tag,database),
call(record(X,Y),Tag).
% Note the use of var/1 to guarantee that no
% outer structure references occur during call/2.
get_atom(X) :-
repeat,
writename('> '),
read(X),
(atom(X) -> true ;
(writename('Data must be an atom, please re-type'),
nl,fail)).
get_keys(List) :-
get_atom(X),
(X == end -> List = [] ;
(get_keys(Rest),
List = [X|Rest])).
end.
structure search1/searchsig =
struct
fun item = dataoperations:record.
ismatch(X,item(A,B),A) :-
B =.. [_|Rest], % _ would actually be database:data/0
member(X,Rest).
end.
structure search2/searchsig =
struct
fun item = dataoperations:record.
ismatch(X,item(A,B),A) :-
match_args(1,B,X).
match_args(Arg,B,X) :-
arg(Arg,B,Item),
(Item = X -> true ;
(Narg is Arg + 1,
match_args(Narg,B,X))).
end.
functor time(x/searchsig) =
struct
structure search = x.
inherit dataoperations.
timesearch(SF) :-
cputime(X),
get_results(SF),
cputime(Y),
Diff is Y - X,
writename('Time taken is '),
writename(Diff), nl.
get_results(SF) :-
dataoperations:get_record(X),
search:ismatch(SF,X,A),
tab(8),writename('Found '),
writename(A),
tab(5),
write([A]),
nl,fail.
get_results(_).
end.
functor menu(x/dataopsig,y/searchsig,z/searchsig) =
struct
structure dataops = x.
structure search1 = time(y).
structure search2 = time(z).
menu :-
nl,
writename('Select option required'), nl,
writename(' 1 - Add new record'), nl,
writename(' 2 - Timed search 1'), nl,
writename(' 3 - Timed search 2'), nl,
writename(' 4 - Quit'), nl,
writename('{Terminate all input with a period (.)}'), nl,
repeat,
writename('> '),
read(X),
valid_choice(X),
menu.
valid_choice(1) :-
dataops:add_record.
valid_choice(2) :-
get_search_key(Key),
search1:timesearch(Key).
valid_choice(3) :-
get_search_key(Key),
search2:timesearch(Key).
valid_choice(4) :-
abort.
valid_choice(_) :-
writename('Invalid choice - reselect'), nl, fail.
get_search_key(Key) :-
repeat,
writename('Type search key > '),
read(X),
(atom(X) -> (structure(Tag,database),
dismantle_name(X,Name,_),
dismantle_name(Key,Name,Tag)) ;
(writename('Search key must be an atom'),
nl,fail)).
% Note here that we have to move the atom typed from
% the current structure to the structure 'database'
% as the original data was moved to 'database' and
% the tags must match for the atoms to be equal.
end.
structure program = menu(dataoperations,search1,search2).